const { ErrorModel, SuccessModel } = require('../utils/index')
const logger = require('../utils/logger')
const { DataAPIModel } = require('./../models/data_api')
const { requestApi } = require('../utils/index')
class DataAPIController {
  /**
   * @description 添加API
   * @param {*} name
   * @param {*} server
   * @param {*} methods
   * @param {*} remark
   * @param {*} Params
   * @param {*} Headers
   * @param {*} Body
   */
  static async dataAPIAdd (req, res, next) {
    const { name, server, methods, remark, Params, Headers, Body } = req.body
    const { user_id: op_user_id } = req.user
    logger.info(`DataAPIController`, `dataAPIAdd`, `req.body`, req.body)
    if (!name) {
      res.json(new ErrorModel([], `请输入API名称`))
      return
    }
    try {
      const APIInfo = await DataAPIModel.findByName(name)
      if (APIInfo) {
        res.json(new ErrorModel([], `当前API已存在`))
        return
      }
      const result = await DataAPIModel.create({
        name,
        server,
        methods,
        remark,
        Params,
        Headers,
        Body,
        op_user_id
      })
      logger.info(`DataAPIController`, `dataAPIAdd`, `result`, result)
      res.json(new SuccessModel([], `新增API成功`))
    } catch (error) {
      logger.error(`DataAPIController`, `dataAPIAdd`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description 更新API
   * @param {*} data_api_id
   * @param {*} name
   * @param {*} server
   * @param {*} methods
   * @param {*} remark
   * @param {*} Params
   * @param {*} Headers
   * @param {*} Body
   */
  static async dataAPIUpd (req, res, next) {
    const {
      data_api_id,
      name,
      server,
      methods,
      remark,
      Params,
      Headers,
      Body,
    } = req.body
    const { user_id: op_user_id } = req.user
    logger.info(`DataAPIController`, `dataAPIUpd`, `req.body`, req.body)
    if (!data_api_id || !name) {
      res.json(new ErrorModel([], `请输入必填项`))
      return
    }
    try {
      const APIInfo = await DataAPIModel.findByIdAndName(data_api_id, name)
      if (APIInfo) {
        res.json(new ErrorModel([], `API已存在`))
        return
      }
      const result = await DataAPIModel.update(
        data_api_id,
        name,
        server,
        methods,
        remark,
        Params,
        Headers,
        Body,
        op_user_id
      )
      logger.info(`DataAPIController`, `dataAPIUpd`, `result`, result)
      res.json(new SuccessModel([], `API更新成功`))
    } catch (error) {
      logger.error(`DataAPIController`, `dataAPIUpd`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description 删除API
   * @param {*} data_api_id
   */
  static async dataAPIDel (req, res, next) {
    const { data_api_id } = req.body
    const { user_id: op_user_id } = req.user
    logger.info(`DataAPIController`, `dataAPIDel`, `req.body`, req.body)
    if (!data_api_id) {
      res.json(new ErrorModel([], `data_api_id 获取失败`))
      return
    }
    try {
      const result = await DataAPIModel.delete(data_api_id, op_user_id)
      logger.info(`DataAPIController`, `dataAPIDel`, `result`, result)
      res.json(new SuccessModel([], `删除API成功`))
    } catch (error) {
      logger.error(`DataAPIController`, `dataAPIDel`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }

  /**
   * @description API分页列表
   * @param {*} data_api_id
   */
  static async dataAPIList (req, res, next) {
    const { limit, size = 1, name, server, methods, remark } = req.query
    logger.info(`DataAPIController`, `dataAPIList`, `req.query`, req.query)
    try {
      const { result, count } = await DataAPIModel.list(
        limit,
        size,
        name,
        server,
        methods,
        remark
      )
      logger.info(`DataAPIController`, `dataAPIList`, `result`, result, count)
      res.json(
        new SuccessModel(
          {
            result,
            total: count[0].count,
            limit: Number(limit),
            size: Number(size),
          },
          `获取API数据成功`
        )
      )
    } catch (error) {
      logger.error(`DataAPIController`, `dataAPIList`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description 测试API
   * @param {*} name
   * @param {*} server
   * @param {*} methods
   * @param {*} remark
   * @param {*} Params
   * @param {*} Headers
   * @param {*} Body
   */
  static async dataAPICheck (req, res, next) {
    const { name, server, methods, Headers, Body } = req.body
    logger.info(`DataAPIController`, `dataAPICheck`, `req.body`, req.body)
    if (!name) {
      res.json(new ErrorModel([], `请输入API名称`))
      return
    }
    try {
      requestApi({ server, methods, Headers, Body }).then(response => {
        res.json(new SuccessModel(response, `查询成功`))
      }).catch(error => {
        logger.error(`DataAPIController`, `dataAPICheck`, `error`, error)
        res.json(new ErrorModel([], error.message))
      })
    } catch (error) {
      logger.error(`DataAPIController`, `dataAPICheck`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description 获取API数据
   * @param {*} data_api_id
   */
  static async dataAPIByID (req, res, next) {
    const { data_api_id } = req.body
    logger.info(`DataAPIController`, `dataAPIByID`, `req.body`, req.body)
    if (!data_api_id) {
      res.json(new ErrorModel([], `查询失败,获取不到data_api_id`))
      return
    }
    try {
      const APIInfo = await DataAPIModel.findById(data_api_id)
      if (!APIInfo) {
        res.json(new ErrorModel([], `查询失败`))
        return
      }
      requestApi(APIInfo).then(response => {
        res.json(new SuccessModel(response, `查询成功`))
      }).catch(error => {
        logger.error(`DataAPIController`, `dataAPIByID`, `error`, error)
        res.json(new ErrorModel([], error.message))
      })
    } catch (error) {
      logger.error(`DataAPIController`, `dataAPIByID`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description API总列表
   * @param {*} data_api_id
   */
  static async dataAllAPIList (req, res, next) {
    try {
      const result = await DataAPIModel.all()
      logger.info(`DataAPIController`, `dataAllAPIList`, `result`, result)
      res.json(new SuccessModel({ result }, `获取API数据成功`))
    } catch (error) {
      logger.error(`DataAPIController`, `dataAllAPIList`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
}

module.exports = {
  DataAPIController,
}
